package com.itheima.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.StudentService;
import com.itheima.aop.Log;
import com.itheima.controller.DeptController;
import com.itheima.entity.PageBean;
import com.itheima.entity.Student;
import com.itheima.entity.StudentQueryParam;
import com.itheima.mapper.StudentMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(StudentService.class);

    @Autowired
    private StudentMapper studentMapper;

    @Log
    @Transactional //开启事务
    @Override
    public PageBean page(StudentQueryParam param) {
        // 1.设置分页参数
        PageHelper.startPage(param.getPage(), param.getPageSize());

        // 2.调用mapper的列表查询方法
        List<Student> studentList = studentMapper.page(param);
        Page p = (Page) studentList;

        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public Student getByStudentId(Integer id) {
        return studentMapper.getByStudentId(id);
    }

    /**
     * 删除多个学员
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        log.info("删除学员：{}", ids);
        studentMapper.deleteBatch(ids);
    }

    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());

        studentMapper.add(student);
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void UpdateScore(Integer id, Integer score) {
        LocalDateTime updateTime = LocalDateTime.now();
        studentMapper.UpdateScore(id, score, updateTime);
    }
}
